﻿angular.module('app')
    .directive('textTransform', function () {
        var transformConfig = {
            bool: function (input, type) {
                switch (type) {
                    case 'parse':
                        if (input == true) {
                            return 1;
                        } else {
                            return 0;
                        }
                        break;
                    case 'format':
                        if (input == 1) {
                            return true;
                        } else {
                            return false;
                        }
                        break;
                    default:
                        return input;
                        break;
                }
            }
        };
        return {
            require: 'ngModel',
            link: function (scope, element, iAttrs, modelCtrl) {
                var transform = transformConfig[iAttrs.textTransform];
                if (transform) {
                    modelCtrl.$parsers.push(function (input) {
                        return transform(input, 'parse');
                    });
                    modelCtrl.$formatters.push(function (input) {
                        return transform(input, 'format');
                    });
                    element.css("text-transform", iAttrs.textTransform);
                }
            }
        };
    });